<?php

/**
 * 用户反馈建议
 *
 * @author suning <snsnsky@gmail.com>
 */
use Common\Bridge;

define('SITE_AUTH', 'report-useradvice');
require('../../../common.inc.php');

class useradvice extends \lib\core\AdminAction
{
    public function doDefault()
    {
        $smarty = $this->app->page();
        $page = max(1, intval($_GET['page']));

        $offset = ($page - 1) * self::PAGE_SIZE;
        $pagesize = self::PAGE_SIZE;

        $pdo = Bridge::pdo(Bridge::DB_CENTER_W);
        $query = $pdo->prepare('select count(*) from UserAdvice');
        $query->execute();
        $total = $query->fetchColumn();

        $query = $pdo->prepare("select * from UserAdvice order by AdviceId DESC limit $offset, $pagesize");
        $query->execute();
        $useradvice = $query->fetchAll();
        $pagination = $this->pagination($total, self::PAGE_SIZE, $page);
        $userids = '';
        foreach ($useradvice as $v) {
            if ($v['UserId'] > 0 && !$v['Contact']) {
                $userids .= $v['UserId'] . ',';
            }
        }
        $userids = trim($userids, ',');
        if ($userids) {
            $pdo = Bridge::pdo(Bridge::DB_PASSPORT_W);
            $query = $pdo->prepare("select UserId,Mobile from `User` WHERE UserId IN ($userids)");
            $query->execute();
            $mobile = $query->fetchAll();
            if (!empty($mobile)) {
                foreach ($mobile as $v) {
                    $mobiles[$v['UserId']] = $v['Mobile'];
                }
                foreach ($useradvice as $k => $v) {
                    $useradvice[$k]['Contents'] = strip_tags($v['Contents']);
                    if ($mobiles[$v['UserId']] && !$v['Contact']) {
                        $useradvice[$k]['Contact'] = $mobiles[$v['UserId']];
                    }
                }
            }
        }
        $smarty->value('useradvice', $useradvice);
        $smarty->value('page', $pagination);
        $smarty->output('report/useradvice.tpl');
    }
}

$app->run();